
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Forms
Imports System.Drawing

'Namespace ClassGridViewGridView
''' 
''' This class extends the the DataGridView so row numbers will 
''' automatically appear in the row header cells. In this 
''' implementation, the width of the column that contains the row 
''' header cells is automatically adjusted to accomodate the row 
''' numbering.
''' ******************************************************************
'''  AUTHOR: Daniel S. Soper
'''     URL: http://www.danielsoper.com
'''    DATE: 20 February 2007
''' LICENSE: Public Domain. Enjoy!   :-)
''' ******************************************************************
''' 
Class ClassGridView
    Inherits DataGridView
    'perform any necessary customization initialization here
    Public Sub New()
        'RowHeadersVisible = False
    End Sub
    'end default constructor
    Protected Overrides Sub OnRowPostPaint(ByVal e As DataGridViewRowPostPaintEventArgs)
        'this method overrides the DataGridView's RowPostPaint event 
        'in order to automatically draw numbers on the row header cells
        'and to automatically adjust the width of the column containing
        'the row header cells so that it can accommodate the new row
        'numbers,
        'store a string representation of the row number in 'strRowNumber'
        Dim strRowNumber As String = (e.RowIndex + 1).ToString()

        'prepend leading zeros to the string if necessary to improve
        'appearance. For example, if there are ten rows in the grid,
        'row seven will be numbered as "07" instead of "7". Similarly, if 
        'there are 100 rows in the grid, row seven will be numbered as "007".
        While strRowNumber.Length < Me.RowCount.ToString().Length
            strRowNumber = "0" & strRowNumber
        End While

        'determine the display size of the row number string using
        'the DataGridView's current font.
        Dim size As SizeF = e.Graphics.MeasureString(strRowNumber, Me.Font)

        'adjust the width of the column that contains the row header cells 
        'if necessary
        If Me.RowHeadersWidth < CInt(Math.Truncate(size.Width + 20)) Then
            Me.RowHeadersWidth = CInt(Math.Truncate(size.Width + 20))
        End If

        'this brush will be used to draw the row number string on the
        'row header cell using the system's current ControlText color
        Dim b As Brush = SystemBrushes.ControlText

        'draw the row number string on the current row header cell using
        'the brush defined above and the DataGridView's default font
        e.Graphics.DrawString(strRowNumber, Me.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2))

        'call the base object's OnRowPostPaint method
        MyBase.OnRowPostPaint(e)
    End Sub
    'end OnRowPostPaint method

    Public Overrides Function GetClipboardContent() As DataObject
        If Me.SelectedRows.Count > 0 And Me.AreAllCellsSelected(False) Then
            'Me.SetSelectedColumnCore(0, False)
            Me.SetSelectedRowCore(Me.Rows.Count - 1, False)
        End If
        Return MyBase.GetClipboardContent()
    End Function
End Class

'End Namespace
'end namespace
